package cn.linran._01_synchronization;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class SyncProducer {
    public static void main(String[] args) throws Exception {
        //1.0创建一个消息生产者
        DefaultMQProducer producer = new DefaultMQProducer("sync_group_name");
        //1.1设置NameServer地址
        producer.setNamesrvAddr("localhost:9876");
        //1.2启动Producer实例
        producer.start();
        for (int i = 0; i < 5; i++) {
            //1.3创建消息，指定Topic，Tag和消息体
            Message message = new Message("Topic_sync", "Tag_sync", ("Hello RocketMQ_sync" + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            //1.4发送消息到一个Broker
            SendResult send = producer.send(message);
            System.out.printf("%s%n",send);
        }
        //1.5如果不再发送消息，关闭Produce实例。
        producer.shutdown();
    }
}
